<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use App\Server\CurlApi;
use App\Utils\Excel as ExcelUtils;
use Hyperf\DbConnection\Db;
use App\Utils\ApiUtils;
use App\Utils\ServerUtil;
use App\Model\Channel;
use App\Nats\Publish\ProxyNats;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\RequestMapping;

class IndexController extends AbstractController
{
    public function index()
    {

        ServerUtil::info(['s'=>566,'oo'=>78787]);
        return 1;
        //找出所有可用的下游渠道
        $api_list = Channel::query()->where('status',1)->get()->toArray();
        //随机取一个渠道
        $api = ApiUtils::randByArray($api_list);
        $info = \json_decode($api['desc'],true);
        $objname = '\\App\Server\\UpstreamList\\'.ucfirst($info['api_name']);
        $options['base_uri'] = $info['base_uri'];
        $obj = new $objname($options);
        $data['merchantNo'] = $info['merchantNo'];
        $data['appkey'] = $info['merchantNo'];
        $data['channel'] = 1;
        $data['money'] = 1;
        $data['callbackUrl'] = '';
        $data['orderNo'] = ServerUtil::orderNo();
        $mobile = ServerUtil::getMobileInfo('13177917095');
        $data['phone'] = $mobile['phone'];
        $data['phoneType'] = $mobile['phoneType'];
        $data['phoneArea'] = $mobile['phoneArea'];
        $data['path'] = $info['path'];
        $data = $obj->createOrder($data);
        
        var_dump($data);
        return 'ok';
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();
        $curl = new CurlApi(); 
        //$curl = make(CurlApi::class,['timeout'=>30,'base_uri' => 'http://10.10.0.55:9501']);
        $data = $curl->post("http://10.10.0.55:9501/test",['timeout'=>30,'user' => 'http://10.10.0.55:9501']);
        
        return $data['data'];
    }

    public function test()
    {
        $curl = new CurlApi(); 
        //$curl = make(CurlApi::class,['timeout'=>30,'base_uri' => 'http://10.10.0.55:9501']);
        $data = $curl->get("https://api.weixin.qq.com/sns/jscode2session?appid=wx861ff7300b79f3b1&secret=190bc0dba81da97163311478c7e25f8f&js_code=043es0Vr1ScSWj0GTrUr1o73Vr1es0Vu&grant_type=authorization_code");
        return $data;
        $data['type'] = $this->request->input('type','');
        $data['openid'] = $this->request->input('openid','');
        $obj = $this->container->get(ProxyNats::class);
        $msg = $obj->sync(json_encode($data),'sg_phonecode.recharge');
        return $msg;

        var_dump($this->request->all());

        return [
            'errorCode' => 5,
            'errorMsg' => "999999",
            "data" =>[
                "id" => "1210",
            ],
        ];
    }

    /** 写入excel测试数据 使用已存在路径测试ok 访问 http://url/import
     * @param bool force
     * @return array
     * Abell 2020/5/20
     */
    public function import(RequestInterface $request){
        $excel_file = $request->file('excel');
        $excel_read = make(ExcelUtils::class)->excel($excel_file);
        return $excel_read;
    }

    /** 导出excel文档  ==> ok 访问 http://url/export
     * @param array $heads 标题头数组
     * @param array $data 表格内容数组
     * @return str 文档下载链接url
     * Abell 2020/5/19
     */
    public function export(RequestInterface $request){
        $heads = [
            'titile' => '微信用户表',
            'keys_array' => ['openid' => 'openid' , 'nickname' => '呢称' ,'avatarurl' => '头像' ,'gender' => '性别','city' => '市区']
        ];
        $data = [
            ['openid' => 'openid1' , 'nickname' => '呢称1' ,'avatarurl' => '头像' ,'gender' => '性别','city' => '市区'],
            ['openid' => 'openid2' , 'nickname' => '呢称2' ,'avatarurl' => '头像' ,'gender' => '性别','city' => '市区'],
            ['openid' => 'openid3' , 'nickname' => '呢称3' ,'avatarurl' => '头像' ,'gender' => '性别','city' => '市区'],
        ];
        return make(ExcelUtils::class)->output($heads,$data);
    }

}
